package com.yifeng.repo.base.mdm.console.impl;

import com.yifeng.base.account.api.UserMasterDataRestService;
import com.yifeng.base.account.api.dto.*;
import com.yifeng.base.account.api.param.UserParam;
import com.yifeng.repo.base.mdm.loader.annotation.MdmCache;
import com.yifeng.repo.base.params.ConsoleParam;
import com.yifeng.repo.base.security.proxy.CommonProxy;
import com.yifeng.repo.base.utils.codec.URLEncodeHelper;
import com.yifeng.repo.base.utils.converter.DateHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by daibing on 2024/1/30.
 */
@MdmCache(mdmTypeCode = "user")
public class UserMasterDataRestServiceImpl extends CommonProxy implements UserMasterDataRestService {
    private final String mapping;

    public UserMasterDataRestServiceImpl(ConsoleParam consoleParam) {
        super(consoleParam);
        this.mapping = "/yfcloud-user/rest/user";
    }

    @Override
    public List<UserCoreInfoDto> listCoreInfo(UserParam userParam) {
        String url = consoleParam.getUrl() + mapping + "/listCoreInfo";
        Map<String, String> params = new HashMap<>();
        params.put("userCode", userParam.getUserCode());
        params.put("realUserCode", userParam.getRealUserCode());
        params.put("orgCode", userParam.getOrgCode());
        params.put("storeCode", userParam.getStoreCode());
        params.put("postCode", userParam.getPostCode());
        params.put("positionCode", userParam.getPositionCode());
        params.put("userName", userParam.getUserName());
        params.put("userType", userParam.getUserType() != null ? userParam.getUserType().getCode() : null);
        params.put("userStatus", userParam.getUserStatus() != null ? userParam.getUserStatus().getCode() : null);
        params.put("endTime", URLEncodeHelper.encode(DateHelper.getDateTime(userParam.getEndTime())));
        params.put("offsetStartTime", URLEncodeHelper.encode(DateHelper.getDateTime(userParam.getOffsetStartTime())));
        params.put("offsetUserCode", userParam.getOffsetUserCode());
        params.put("pageSize", userParam.getPageSize() != null ? userParam.getPageSize().toString() : null);
        String queryParameters = buildQueryParameters(params);
        String postCodesQueryParameters = buildCollectionQueryParameters("postCodes", userParam.getPostCodes(), true);
        String requestUrl = String.format("%s?%s", url, queryParameters + postCodesQueryParameters);
        return this.doListCallByRpc(requestUrl, UserCoreInfoDto.class);
    }

    @Override
    public UserBaseInfoDto getBaseInfoByCode(String userCode) {
        String url = consoleParam.getUrl() + mapping + "/getBaseInfoByCode";
        String queryParameters = buildSingleQueryParameters("userCode", userCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doGetCallByRpc(requestUrl, UserBaseInfoDto.class);
    }

    @Override
    public List<UserBaseInfoDto> listBaseInfoByCodes(List<String> userCodes) {
        String url = consoleParam.getUrl() + mapping + "/listBaseInfoByCodes";
        String queryParameters = buildCollectionQueryParameters("userCodes", userCodes);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, UserBaseInfoDto.class);
    }

    @Override
    public UserBaseInfoDto getOwnerInfoByStoreCode(String storeCode) {
        String url = consoleParam.getUrl() + mapping + "/getOwnerInfoByStoreCode";
        String queryParameters = buildSingleQueryParameters("storeCode", storeCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doGetCallByRpc(requestUrl, UserBaseInfoDto.class);
    }

    @Override
    public UserBaseInfoDto getBaseInfoByMobile(String mobile) {
        String url = consoleParam.getUrl() + mapping + "/getBaseInfoByMobile";
        String queryParameters = buildSingleQueryParameters("mobile", mobile);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doGetCallByRpc(requestUrl, UserBaseInfoDto.class);
    }

    @Override
    public List<UserBaseInfoDto> listBaseInfoByStoreCode(String storeCode) {
        String url = consoleParam.getUrl() + mapping + "/listBaseInfoByStoreCode";
        String queryParameters = buildSingleQueryParameters("storeCode", storeCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, UserBaseInfoDto.class);
    }

    @Override
    public List<UserBaseInfoDto> listBaseInfoByOrgCode(String orgCode) {
        String url = consoleParam.getUrl() + mapping + "/listBaseInfoByOrgCode";
        String queryParameters = buildSingleQueryParameters("orgCode", orgCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, UserBaseInfoDto.class);
    }

    @Override
    public UserDetailInfoDto getDetailInfoByCode(String userCode) {
        String url = consoleParam.getUrl() + mapping + "/getDetailInfoByCode";
        String queryParameters = buildSingleQueryParameters("userCode", userCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doGetCallByRpc(requestUrl, UserDetailInfoDto.class);
    }

    @Override
    public List<UserDetailInfoDto> listDetailInfoByCodes(List<String> userCodes) {
        String url = consoleParam.getUrl() + mapping + "/listDetailInfoByCodes";
        String queryParameters = buildCollectionQueryParameters("userCodes", userCodes);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, UserDetailInfoDto.class);
    }

    @Override
    public List<UserTitleInfoDto> listUserTitleByCode(String userCode) {
        String url = consoleParam.getUrl() + mapping + "/listUserTitleByCode";
        String queryParameters = buildSingleQueryParameters("userCode", userCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, UserTitleInfoDto.class);
    }

    @Override
    public AdmOrgUserRelation getOrgRelationByUserCode(String userCode) {
        String url = consoleParam.getUrl() + mapping + "/getOrgRelationByUserCode";
        String queryParameters = buildSingleQueryParameters("userCode", userCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doGetCallByRpc(requestUrl, AdmOrgUserRelation.class);
    }

    @Override
    public List<UserBaseInfoDto> listSubUserByUserCode(UserParam userParam) {
        String url = consoleParam.getUrl() + mapping + "/listSubUserByUserCode";
        Map<String, String> params = new HashMap<>();
        params.put("userCode", userParam.getUserCode());
        params.put("realUserCode", userParam.getRealUserCode());
        params.put("orgCode", userParam.getOrgCode());
        params.put("storeCode", userParam.getStoreCode());
        params.put("postCode", userParam.getPostCode());
        params.put("positionCode", userParam.getPositionCode());
        params.put("userName", userParam.getUserName());
        params.put("userType", userParam.getUserType() != null ? userParam.getUserType().getCode() : null);
        params.put("userStatus", userParam.getUserStatus() != null ? userParam.getUserStatus().getCode() : null);
        params.put("endTime", URLEncodeHelper.encode(DateHelper.getDateTime(userParam.getEndTime())));
        params.put("offsetStartTime", URLEncodeHelper.encode(DateHelper.getDateTime(userParam.getOffsetStartTime())));
        params.put("offsetUserCode", userParam.getOffsetUserCode());
        params.put("pageSize", userParam.getPageSize() != null ? userParam.getPageSize().toString() : null);
        String queryParameters = buildQueryParameters(params);
        String postCodesQueryParameters = buildCollectionQueryParameters("postCodes", userParam.getPostCodes(), true);
        String requestUrl = String.format("%s?%s", url, queryParameters + postCodesQueryParameters);
        return this.doListCallByRpc(requestUrl, UserBaseInfoDto.class);
    }
}
